import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_4/News.dart';
import 'package:flutter_4/NewsDetail.dart';
import 'package:flutter_4/main.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _HomeScreenState();
  }
}
class _HomeScreenState extends State<HomeScreen>
    with SingleTickerProviderStateMixin {
  List<News>_datas=[];
  bool _cancelConnect=false;
  @override
  void initState(){
    getDates().then((List<News> datas){
      if(!_cancelConnect){
        setState(() {
          _datas=datas;
        });
      }
    }).catchError((e){
      print('error$e');
    }).whenComplete((){
      print('新闻获取完毕');
    }).timeout(Duration(seconds: 5)).catchError((timeOut){
      print('超时:${timeOut}');
      _cancelConnect=true;
    });


  }

  Future<Null> _handlerRefresh() async {

    await new Future.delayed(new Duration(seconds: 2));
    setState(() {
      _datas.addAll(_datas);
    });
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length:3,
        child: Scaffold(
          appBar:  AppBar(
            toolbarHeight: 110.0,
            title: new Text('新闻资讯'),
            centerTitle: true,
            actions: [
              IconButton(
                icon:Icon(Icons.add_a_photo),
                onPressed: (){
                  Navigator.of(context).push(
                      MaterialPageRoute(builder:(context){
                        return Image.asset('images/lake.jpg');
                      })
                  );
                },
              )
            ],
            bottom: TabBar(
                tabs:<Widget>[
                  Tab(text:"综合"),
                  Tab(text:"经济"),
                  Tab(text:"娱乐"),
                ]


            ),

            leading: Icon(Icons.menu),
            backgroundColor:Colors.blue,
          ),
          body:Container(
            child:RefreshIndicator(
              child: ListView.builder(
                itemCount: _datas.length,
                itemBuilder: (BuildContext context,int index){
                  return Card(
                    color: Colors.grey[250],
                    elevation: 5.0,
                    child: Builder(
                        builder: (context)=>InkWell(
                          child:new Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Image.network(_datas[index].picUrl as String,fit:BoxFit.fitWidth),
                              Padding(
                                padding: const EdgeInsets.all(10.0),
                                child: Text(
                                  _datas[index].title.toString(),
                                  style:TextStyle(
                                    fontSize: 16.0,
                                    fontWeight: FontWeight.bold,
                                  )  ,
                                ),
                              ),
                              Padding(
                                padding: _datas[index].description.toString().isEmpty
                                    ? const EdgeInsets.all(0.0)
                                    :const EdgeInsets.only(
                                    left:10.0,right: 10.0,bottom: 10.0
                                ),
                                child: Text(
                                  _datas[index].description as String,
                                  style:TextStyle(
                                    fontSize: 12.0,
                                  )  ,
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.only(left:10.0,right: 10.0),
                                child: Text(
                                  '时间：${_datas[index].ctime}',
                                  style:TextStyle(
                                    fontSize: 12.0,

                                  )  ,
                                ),
                              ),
                            ],
                          ),
                          onTap: ()=>Navigator.of(context).push(MaterialPageRoute(
                              builder: (context)=>NewsDetail(
                                url:_datas[index].url.toString(),
                                title: _datas[index].title.toString(),
                              )
                          )),
                        )),
                  );
                },
              ),onRefresh: () => _handlerRefresh()
            )
          ) ,
        ),




      );


  }
}